<?php

namespace addons\TinyShare\api\modules\v1\controllers;

use Yii;
use common\helpers\AddonHelper;
use common\enums\StatusEnum;
use api\controllers\OnAuthController;
use addons\TinyShare\common\models\ShareAdv;
use addons\TinyShare\common\models\ShareContent;
use addons\TinyShare\common\models\forms\ContentSearch;

/**
 * 首页相关
 *
 * Class IndexController
 * @package addons\TinyShare\api\modules\v1\controllers
 * @author jackey <270499458@qq.com>
 */
class IndexController extends OnAuthController
{
    public $modelClass = '';

    /**
     * 不用进行登录验证的方法
     *
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     *
     * @var array
     */
    protected $authOptional = ['index','top-cate-list','content-list'];

    /**
     * @return array|\yii\data\ActiveDataProvider
     */
    public function actionIndex()
    {
        $member_id = !Yii::$app->user->isGuest ? Yii::$app->user->identity->member_id : '';
		
		$recommend_search = new ContentSearch();
		$recommend_search->position=1;
		$new_search = new ContentSearch();
		$new_search->position=$this->getPositon();
		
        return [
            'adv' => Yii::$app->tinyShareService->adv->getListByLocal(ShareAdv::LOCATION_INDEX), // 广告
            'cate' => Yii::$app->tinyShareService->cate->getTopList(), // 顶级分类
            'recommend' => Yii::$app->tinyShareService->content->getListBySearch($recommend_search,$member_id), //首页推荐
            'new' => Yii::$app->tinyShareService->content->getListBySearch($new_search,$member_id), //最新
        ];
    }
	
	/**
	 * 获取顶级分类
	 */
	public function actionTopCateList(){
		return Yii::$app->tinyShareService->cate->getTopList();
	}
	
	/**
	 * 获取指定分类下的数据
	 */
	public function actionContentList(){
		$member_id = !Yii::$app->user->isGuest ? Yii::$app->user->identity->member_id : '';
		$cate_id = Yii::$app->request->post('cate_id');
		
		$search = new ContentSearch();
		$search->position=$this->getPositon();
		$search->cate_id=$cate_id;
		
		return [
            'cate' => Yii::$app->tinyShareService->cate->findChildsById($cate_id), // 获取所有子分类
            'content' => Yii::$app->tinyShareService->content->getListBySearch($search,$member_id), //首页推荐
        ];
	}
	
	/**
	 * 获取所有推荐位置的最大位或值
	 */
	protected function getPositon(){
		$position_value=0;
		foreach(ShareContent::$positionExplain as $k=>$v){
			if($position_value){
				$position_value=$position_value|$k;
			}else{
				$position_value=$k;
			}
		}
		return $position_value;
	}
}